<template>
  <div>
    <el-card style="width: 600px;">
      <el-form label-width="80px" size="small">
        <el-form-item label="用户名">
          <el-input v-model="form.username" disabled autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" show-password autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="width: 600px">
      <el-button type="success" @click="processHealthRecordStart">开启健康信息记录</el-button>
      <el-button type="danger" @click="processHealthRecordStop">关闭健康信息记录</el-button>
      <el-button type="success" @click="processSleepRecordStart">开启睡眠信息记录</el-button>
      <el-button type="danger" @click="processSleepRecordStop">关闭睡眠信息记录</el-button>
    </el-card>
  </div>
</template>

<script>

import request from "@/utils/request"
import axios from 'axios'

export default {
  name: "Person",
  data() {
    return {
      form: {},
      user: sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : {}
    }
  },
  created() {
    this.getUser().then(res => {
      console.log(res);
      this.form = res;
    })
  },
  methods: {
    async getUser() {
      return (await request.get("/user/search/self/" + this.user.id)).data
      // return (await request.get("/user/search/self/1")).data
    },
    save() {
      request.post("/user/save", this.form).then(res => {
        if (res.code === '0') {
          this.$message.success("保存成功")

          //  触发父级更新User的方法
          // this.$emit("refreshUser")

          //  更新浏览器存储的用户信息
          this.getUser().then(res => {
            // res.token = JSON.parse(localStorage.getItem("user")).token
            sessionStorage.setItem("user", JSON.stringify(res))
          })

        } else {
          this.$message.error("保存失败")
        }
      })
    },
    processHealthRecordStart() {
      request.post("/process/start/healthRecord/" + this.user.id).then(res => {
        if (res.code === '0') {
          this.$message.success("开启成功")
        } else {
          this.$message.error(res.msg)
        }
      }).catch(error => {
        console.error('Error starting process:', error);
      })
    },
    processHealthRecordStop() {
      request.post("/process/stop/healthRecord/" + this.user.id).then(res => {
        if (res.code === '0') {
          this.$message.success("关闭成功")
        } else {
          this.$message.error(res.msg)
        }
      }).catch(error => {
          console.error('Error stopping process:', error);
      })
    },
    processSleepRecordStart() {
      request.post("/process/start/sleepRecord/" + this.user.id).then(res => {
        if (res.code === '0') {
          this.$message.success("开启成功")
        } else {
          this.$message.error(res.msg)
        }
      }).catch(error => {
        console.error('Error starting process:', error);
      })
    },
    processSleepRecordStop() {
      request.post("/process/stop/sleepRecord/" + this.user.id).then(res => {
        if (res.code === '0') {
          this.$message.success("关闭成功")
        } else {
          this.$message.error(res.msg)
        }
      }).catch(error => {
        console.error('Error stopping process:', error);
      })
    },
  }
}
</script>

<style>
.avatar-uploader {
  text-align: center;
  padding-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
.avatar {
  width: 138px;
  height: 138px;
  display: block;
}
</style>